import { pick } from 'lodash';
import { ref, onBeforeMount, inject, getCurrentInstance, computed, watch } from 'vue';
// import useCommon from './common';
import useCommon from '../common/common';
// const {useCommon} = tyRoot.getExportResource('@/modules/rpm/business-module/project/components/composables/common.js');
// 创建 props
export const createProps = (op = {}) => ({
  projectDetail: {
    type: Object,
    required: true
  },
  ...op
});
// console.log("😝😝😝")
export const useProjectEdit = props => {
  const { commandFunc } = inject('apiFunTools');
  const formData = ref({});
  const { proxy } = getCurrentInstance();
  // 表单组件实例对象
  const conponentRefs = ref({});
  const setComponentRef = (key, el) => {
    conponentRefs.value[key] = el;
  };
  // 附件默认值
  const defaultFileList = ref([]);
  // 密级配置
  const securityLabelsOptions = ref({
    list: [],
    placeholder: proxy.$t('plsSelect'),
    props: { label: 'displayName', value: 'name' }
  });
  const activeNames = ref(['basic', 'system']);
  const list = ref([
    {
      header: proxy.$t('basic.label.regularProperties'),
      name: 'basic',
      key: 'basic'
    }
  ]);
  const systemList = ref([
    {
      header: proxy.$t('basic.label.systemProperties'),
      name: 'system',
      key: 'system'
    }
  ]);
  const {
    formRef,
    ibaForm,
    tyProjectForm,
    phaseOptions,
    isAddparameter,
    getPhaseList,
    getNotSpecialChar,
    validatorName,
    valideIbaForm,
    getIbaForm
  } = useCommon(formData, true);
  const getUserName = (fullnameKey, nameKey) => {
    const fullname = formData.value[fullnameKey];
    const name = formData.value[nameKey];
    return `${fullname || ''}${name ? `(${name})` : ''}`;
  };
  // 表单校验规则
  const rules = ref({
    name: [{ required: true, validator: validatorName, trigger: 'blur' }],
    description: [
      {
        trigger: 'blur',
        max: 1024,
        message: proxy.$t('basic.message.maxLength', {
          length: 1024
        })
      }
    ]
  });
  // 获取密级下拉列表
  const getSecurityLabelsList = () => {
    const callback = res => {
      securityLabelsOptions.value.list = res;
    };
    commandFunc('$rpmapis', 'project', 'queryAllEnumerationItemByDefName', 'SecurityLabels', {
      callback
    });
  };
  // 获取附件详情
  const getFileListByEntity = () => {
    const data = pick(formData.value, ['oid', 'otype']);
    commandFunc('$rpmapis', 'project', 'getFileListByEntity', data, {
      callback: res => {
        if (res.length) {
          res.forEach(item => {
            item.name = item.fileName;
          });
        }
        defaultFileList.value = res || [];
      }
    });
  };
  // 系统属性配置
  const systemFormList = computed(() => {
    return [
      {
        label: proxy.$t('creator'),
        prop: 'creator',
        componentName: 'ty-plm-tooltip-value',
        options: {
          value: getUserName('creatorDescription', 'creatorName'),
          content: getUserName('creatorDescription', 'creatorName')
        },
        col: 8
      },
      {
        label: proxy.$t('createTime'),
        prop: 'createTime',
        componentName: 'ty-plm-tooltip-value',
        options: { value: formData.value.createstamp, content: formData.value.createstamp },
        col: 8
      },
      {
        label: proxy.$t('table.updatorName'),
        prop: 'updatorName',
        componentName: 'ty-plm-tooltip-value',
        options: {
          value: getUserName('updatorDescription', 'updatorName'),
          content: getUserName('updatorDescription', 'updatorName')
        },
        col: 8
      },
      {
        label: proxy.$t('table.modifyTime'),
        prop: 'modifyTime',
        componentName: 'ty-plm-tooltip-value',
        options: { value: formData.value.updatestamp, content: formData.value.updatestamp },
        col: 8
      }
    ];
  });
  // 常规属性配置
  const basicFormList = computed(() => {
    return [
      {
        label: proxy.$t('projectName'),
        prop: 'name',
        componentName: 'ty-plm-input',
        col: 8
      },
      {
        label: proxy.$t('projectNumber'),
        prop: 'projectNumber',
        componentName: 'ty-plm-tooltip-value',
        options: { value: formData.value.projectNumber, content: formData.value.projectNumber, placement: 'bottom' },
        col: 8
      },
      {
        label: proxy.$t('duration'),
        prop: 'contractDuration',
        componentName: 'ty-plm-input',
        options: {
          dataType: 'number',
          controlsPosition: 'right',
          placeholder: proxy.$t('pls'),
          min: 0
        },
        attrs: {
          style: {
            width: '200px'
          }
        },
        col: 8
      },
      {
        label: proxy.$t('phase'),
        prop: 'phase',
        componentName: 'ty-plm-select',
        options: phaseOptions.value,
        col: 8
      },
      {
        label: proxy.$t('rpm.label.securityLabels'),
        prop: 'securityLabels',
        componentName: 'ty-plm-select',
        options: securityLabelsOptions.value,
        col: 8
      },
      {
        label: proxy.$t('projectType'),
        prop: 'typeName',
        componentName: 'ty-plm-tooltip-value',
        options: { value: formData.value.typeName, content: formData.value.typeName },
        col: 8
      },
      {
        label: proxy.$t('rpm.label.completionStatus'),
        prop: 'completionStatus',
        componentName: 'project-tag-value',
        col: 8
      },
      {
        label: proxy.$t('file'),
        prop: 'file',
        componentName: 'ty-plm-file-upload',
        col: 24,
        refName: 'upload',
        options: {
          multipleSelect: true
        },
        attrs: {
          defaultFileList: defaultFileList.value
        }
      },
      {
        label: proxy.$t('table.description'),
        prop: 'description',
        componentName: 'ty-plm-input',
        options: {
          type: 'textarea',
          rows: 3,
          placeholder: proxy.$t('pls')
        },
        col: 24
      }
    ];
  });
  // 表单回显
  watch(
    () => props.projectDetail,
    newVal => {
      if (newVal?.oid) {
        formData.value = pick(newVal, [
          'oid',
          'otype',
          'name',
          'completionStatus',
          'contractDuration',
          'securityLabels',
          'projectNumber',
          'phase',
          'typeName',
          'typeOid',
          'typeLogicalIdentifier',
          'description',
          'createstamp',
          'creatorDescription',
          'creatorName',
          'updatorDescription',
          'updatorName',
          'updatestamp',
          'healthStatus'
        ]);
      }
      getFileListByEntity();
      if (formData.value.typeLogicalIdentifier) {
        getIbaForm(formData.value.typeLogicalIdentifier);
      }
    },
    {
      deep: true,
      immediate: true
    }
  );
  onBeforeMount(() => {
    getPhaseList();
    getSecurityLabelsList();
    getNotSpecialChar();
  });
  return {
    formData,
    setComponentRef,
    conponentRefs,
    activeNames,
    list,
    systemList,
    formRef,
    ibaForm,
    tyProjectForm,
    isAddparameter,
    valideIbaForm,
    rules,
    systemFormList,
    basicFormList
  };
};
